package android.vstore.activities;

import java.util.ArrayList;

import com.appvn2014.appstorevn.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;

import DAO.NotificationDAO;
import DTO.ImageDTO;
import DTO.NotificationDTO;
import DTO.ProductDTO;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.vstore.fragment.PageSectionFragment.CustomAdapterVerticalProduct;
import android.vstore.fragment.PageSectionFragment.CustomAdapterVerticalProduct.ViewHolderProduct;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import anroid.vstore.provider.Navigator;

public class SceneNotificationsActivity extends BaseActivity implements OnItemClickListener, OnScrollListener {

	public ArrayList<NotificationDTO> _lstItem = new ArrayList<NotificationDTO>();
	public CustomAdapterVerticalCat adapter;
	public ListView lv_lstProds;
	public boolean isProductsLoading;
	private ActionBar actionBar;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scene_notifications);
		
		// get the action bar
		actionBar = getActionBar();

		actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.main)));
		// Enabling Back navigation on Action Bar icon
		actionBar.setDisplayHomeAsUpEnabled(true);
		InitGUI();
		Load();
	}
	public void InitGUI()
	{
		lv_lstProds = (ListView)findViewById(R.id.lv_notf);
		lv_lstProds.setOnItemClickListener(this);
		lv_lstProds.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(), true, false, this));
		
	}
	public void Load()
	{
		(new Loader()).execute(_lstItem.size(),_lstItem.size()+10);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.scene_notifications, menu);
		return true;
	}
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
        	setResult(12);
            finish();
            return true;
       
        }
        return super.onOptionsItemSelected(item);
    }
	public class Loader extends AsyncTask<Integer, Void, ArrayList<NotificationDTO>>{
		@Override
		protected ArrayList<NotificationDTO> doInBackground(Integer... params) {
			
			ArrayList<NotificationDTO> lstProds = NotificationDAO.GetNotifications(params[0], params[1]);
			return lstProds;
		}      
		
		@Override
		protected void onPostExecute(ArrayList<NotificationDTO> lstVipProd) {
			isProductsLoading = false;
			if(_lstItem.size() == 0)
			{	
				adapter = new CustomAdapterVerticalCat(SceneNotificationsActivity.this	, R.layout.item_list_cats	, _lstItem);
				lv_lstProds.setAdapter(adapter);
			}
			if(_lstItem != null && lstVipProd.size() > _lstItem.size())
			{
				int startindx = _lstItem.size();
				_lstItem.addAll(lstVipProd.subList(startindx, lstVipProd.size()));
				
			}
			adapter.notifyDataSetChanged();
		}

		@Override
		protected void onPreExecute() {
			if(isProductsLoading )
			{
				this.cancel(true);
				return;
			}
			isProductsLoading = true;
			
		}

		@Override
		protected void onProgressUpdate(Void... values) {
		}
	}
	
	public class CustomAdapterVerticalCat extends ArrayAdapter<NotificationDTO>
	{
		ArrayList<NotificationDTO> itemList;
	    public Activity context;
	    public LayoutInflater inflater;
		private final String TAG = "CustomAdapterProduct";
		
	    public CustomAdapterVerticalCat(Activity context, int textViewResource, ArrayList<NotificationDTO> itemList) 
	    {
	        super(context, textViewResource, itemList);

	        this.context = context;
	        this.itemList = itemList; 

	        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    } 

	    @Override 
	    public int getCount() 
	    {
	        return itemList.size();
	    }

	    @Override
	    public NotificationDTO getItem(int position) 
	    {
	        return itemList.get(position);
	    }
	    
	    @Override
	    public long getItemId(int position) 
	    {
	        return 0;
	    }

	    public class ViewHolderProduct
	    {
	        public ImageView imgViewLogo;
	        public TextView txtViewTitle;
	        public TextView txtViewTime;
	        public TextView txtViewLocation;
	        public TextView txtViewPrice;
	        public TextView txtViewPromotionPrice;
	        public TextView txtViewContent;
	    }

	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) 
	    {	    	
	    	ViewHolderProduct holder;
	    	if(convertView == null) 
	    	{
	    		holder = new ViewHolderProduct();
	    		convertView = inflater.inflate(R.layout.item_list_nots, null);

	    		holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.iv_itemlistnot_logo);
	    		holder.txtViewTitle = (TextView) convertView.findViewById(R.id.tv_itemlistnot_title);
	    		holder.txtViewContent = (TextView) convertView.findViewById(R.id.tv_itemlistnot_count);
	    		convertView.setTag(holder);
	    	}
	    	else
	    		holder = (ViewHolderProduct)convertView.getTag();

	    	NotificationDTO bean = (NotificationDTO) itemList.get(position);
		    	

	    	
	    	if(bean == null) return convertView;

			ImageDTO.displayImage( bean.imgURL, holder.imgViewLogo);

	    	holder.txtViewTitle.setText(bean.name);
	    	holder.txtViewContent.setText(bean.msg);
	    	return convertView;
	    }
	}
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		if(_lstItem != null)
		{
			if(_lstItem.size()-2 > firstVisibleItem && _lstItem.size()-2 < firstVisibleItem+visibleItemCount)
			{
				Load();
			}
		}
	}
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		Navigator.toDetailItem(this, _lstItem.get(arg2).refId);
	}
}
